package com.stone.mythread.juc;

import java.util.concurrent.CyclicBarrier;

/**
 * 集齐7颗龙珠召唤神龙
 */
public class ThreadCyclicBarrier {

    // 创建固定值
    private static final int number = 7;

    public static void main(String[] args) {
        // 创建 CyclicBarrier
        CyclicBarrier cyclicBarrier = new CyclicBarrier(number, () -> {
            System.err.println("集齐7颗龙珠就可以召唤神龙。");
        });

        // 集齐7颗龙珠
        for (int i = 1; i <= 7; i++) {
            new Thread(() -> {
                System.err.println(Thread.currentThread().getName() + " 星龙珠被收集到了。");
                // 等待
                try {
                    cyclicBarrier.await();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.err.println(Thread.currentThread().getName() + " 星龙珠分散。");
            }, String.valueOf(i)).start();
        }
    }
}
